package com.clayecloud.module_visitor;

import static com.scwang.smartrefresh.layout.util.DensityUtil.dp2px;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.dialog.BaseDialog;
import com.clayecloud.lib_base.dialog.DateDialog;
import com.clayecloud.lib_base.dialog.InputDialog;
import com.clayecloud.lib_base.dialog.SelectDialog;
import com.clayecloud.module_visitor.databinding.ActivityVisitorMainBinding;
import com.clayecloud.module_visitor.viewModels.VisitorModelViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Set;

@Route(path = "/visitor/index")
public class VisitorMainActivity extends AppCompatActivity implements ToastAction {

    private Context context = this;

    private ActivityVisitorMainBinding activityVisitorMainBinding;

    private VisitorModelViewModel visitorModelViewModel;

    private Integer type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityVisitorMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_visitor_main);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        visitorModelViewModel = viewModelProvider.get(VisitorModelViewModel.class);
        // 初始化函数进行初始化
        init();
        // 绑定监听事件
        initListener();
    }
    /**
     * 初始化函数
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        visitorModelViewModel.leaddata();
    }
    /**
     * 绑定监听事件
     */
    public void initListener(){
        // 头部导航监听函数
        activityVisitorMainBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        activityVisitorMainBinding.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SelectDialog.Builder(context)
                        .setTitle("申请类型")
                        .setList("临时", "长期")
                        // 设置单选模式
                        .setSingleSelect()
                        // 设置默认选中
                        .setSelect(0)
                        .setListener(new SelectDialog.OnListener<String>() {
                            @Override
                            public void onSelected(BaseDialog dialog, HashMap<Integer, String> data) {
                                Set keys = data.keySet();
                                type = (Integer) keys.toArray()[0];
                                activityVisitorMainBinding.type.setRightText(data.get(keys.toArray()[0]));
                                activityVisitorMainBinding.type.setRightTextColor(Color.parseColor("#303133"));
                            }
                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.accesstime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 日期选择对话框
                new DateDialog.Builder(context)
                        .setTitle("请选择日期")
                        // 确定按钮文本
                        .setConfirm("确定")
                        // 设置 null 表示不显示取消按钮
                        .setCancel("取消")
                        .setListener(new DateDialog.OnListener() {
                            @Override
                            public void onSelected(BaseDialog dialog, int year, int month, int day) {
                                // 如果不指定时分秒则默认为现在的时间
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(Calendar.YEAR, year);
                                // 月份从零开始，所以需要减 1
                                calendar.set(Calendar.MONTH, month - 1);
                                calendar.set(Calendar.DAY_OF_MONTH, day);
                                activityVisitorMainBinding.accesstime.setRightText(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
                                activityVisitorMainBinding.accesstime.setRightTextColor(Color.parseColor("#303133"));
                            }
                            @Override
                            public void onCancel(BaseDialog dialog) {
                                toast("取消了");
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.endtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 日期选择对话框
                new DateDialog.Builder(context)
                        .setTitle("请选择日期")
                        // 确定按钮文本
                        .setConfirm("确定")
                        // 设置 null 表示不显示取消按钮
                        .setCancel("取消")
                        .setListener(new DateDialog.OnListener() {
                            @Override
                            public void onSelected(BaseDialog dialog, int year, int month, int day) {
                                // 如果不指定时分秒则默认为现在的时间
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(Calendar.YEAR, year);
                                // 月份从零开始，所以需要减 1
                                calendar.set(Calendar.MONTH, month - 1);
                                calendar.set(Calendar.DAY_OF_MONTH, day);
                                activityVisitorMainBinding.endtime.setRightText(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
                                activityVisitorMainBinding.endtime.setRightTextColor(Color.parseColor("#303133"));
                            }
                            @Override
                            public void onCancel(BaseDialog dialog) {
                                toast("取消了");
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("访客姓名（多名;隔开）")
                        .setHint(activityVisitorMainBinding.name.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityVisitorMainBinding.name.getRightText().equals(content)) {
                                activityVisitorMainBinding.name.setRightText(content);
                                activityVisitorMainBinding.name.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("手机号码")
                        .setHint(activityVisitorMainBinding.phone.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityVisitorMainBinding.phone.getRightText().equals(content)) {
                                activityVisitorMainBinding.phone.setRightText(content);
                                activityVisitorMainBinding.phone.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.describe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("访问目的描述")
                        .setHint(activityVisitorMainBinding.describe.getRightText())
                        .setListener((dialog, content) -> {
                            if (!activityVisitorMainBinding.describe.getRightText().equals(content)) {
                                activityVisitorMainBinding.describe.setRightText(content);
                                activityVisitorMainBinding.describe.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        activityVisitorMainBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    visitorModelViewModel.submit(type, activityVisitorMainBinding.accesstime.getRightText().toString(), activityVisitorMainBinding.endtime.getRightText().toString(),
                            activityVisitorMainBinding.name.getRightText().toString(), activityVisitorMainBinding.phone.getRightText().toString(), activityVisitorMainBinding.describe.getRightText().toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

        visitorModelViewModel.getInsertState().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    activityVisitorMainBinding.type.setRightText("");
                    activityVisitorMainBinding.accesstime.setRightText("");
                    activityVisitorMainBinding.endtime.setRightText("");
                    activityVisitorMainBinding.name.setRightText("");
                    activityVisitorMainBinding.phone.setRightText("");
                    activityVisitorMainBinding.describe.setRightText("");
                    goVisitorList();
                }
            }
        });

        activityVisitorMainBinding.visitorList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goVisitorList();
            }
        });
    }

    private void goVisitorList(){
        ARouter.getInstance()
                .build("/visitor/list/index")
                .withTransition(R.anim.window_right_in, R.anim.window_fake)
                .navigation(context);
    }

    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }
}